package com.efounder.form.comp.commodity;

import android.content.Context;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

import com.efounder.form.application.FormViewContainer;

/**
 * Created by XinQing on 2016/10/18.
 *
 * 监听滑动事件和消除与内嵌recyclerview冲突的滚动型
 * http://blog.csdn.net/bin622/article/details/51313654
 *
 * http://blog.csdn.net/u012027644/article/details/51801652
 */

public class InterceptedRecyclerView extends RecyclerView{
    private int downX;
    private int downY;
    private int mTouchSlop;


    public InterceptedRecyclerView(Context context) {
        super(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();//
    }

    public InterceptedRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();//
    }

    public InterceptedRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();//
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
//        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT){
            int action = e.getAction();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    downX = (int) e.getRawX();
                    downY = (int) e.getRawY();
                    // http://www.open-open.com/lib/view/open1474266969512.html
                    int verticalScrollRange = computeVerticalScrollRange();
                    int verticalScrollExtent = computeVerticalScrollExtent();
                    FormViewContainer.setDisallowIntercept(true);
                    if (verticalScrollRange == verticalScrollExtent){
                        FormViewContainer.setDisallowIntercept(false);
                    }else {
                        FormViewContainer.setDisallowIntercept(true);
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    int moveY = (int) e.getRawY();
                    if (Math.abs(moveY - downY) > mTouchSlop) {
                        if (this.getLayoutManager() instanceof LinearLayoutManager){
                            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) getLayoutManager();
                            if ((linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0 && moveY > downY)
                                    || (linearLayoutManager.findLastCompletelyVisibleItemPosition() == getAdapter().getItemCount()-1 && moveY  < downY)){
                                FormViewContainer.setDisallowIntercept(false);
                                return super.onInterceptTouchEvent(e);//顶部和底部不拦截
                            }
                        }
                        Log.i("-----","InterceptedRecyclerView-----onInterceptTouchEvent:" + true);
                        return true;
                    }
                    break;
                default:
                    FormViewContainer.setDisallowIntercept(false);
                    break;
            }
//        }
        return super.onInterceptTouchEvent(e);
    }


}
